import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/utils/md5_utils.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/input_card_widget.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//新密码页
class PasswordSubmissionPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PasswordSubmissionState();
  }
}

class PasswordSubmissionState extends State<PasswordSubmissionPage> {
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        backgroundColor: Color(0XFF00A5F1),
        automaticallyImplyLeading: false, //设置没有返回按钮
        title: Text(
          "修改密码",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
      ),
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            InputCardWidget(
              inputFormatters: [
                LengthLimitingTextInputFormatter(16),
              ],
              hintText: "请输入新密码",
              title: "输入密码",
              isPassWord: true,
              contro: controller0,
            ),
            InputCardWidget(
              inputFormatters: [
                LengthLimitingTextInputFormatter(16),
              ],
              hintText: "请输入新密码",
              title: "再次输入",
              isPassWord: true,
              contro: controller1,
            ),
            Expanded(
                child: Container(
                    margin: EdgeInsets.only(left: 15),
                    child: Text(
                      "密码为8~16位，必须包含数字、字母、符号中至少2种元素",
                      style: TextStyle(fontSize: 9, color: Colors.red),
                    ))),
            Container(
              width: double.infinity,
              height: 48,
              child: FlatButton(
                color: Color(0xFF00A5F1),
                child: Text(
                  "确定",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () {
                  if (controller0.text == controller1.text) {
                    if (controller0.text.length > 7 &&
                        controller1.text.length > 7) {
                      print(isCorrect(controller0.text));
                      if (isCorrect(controller0.text)) {
                        onSubmission(md5Funs(controller0.text));
                      } else {
                        TostUtil.showToast("必须包含数字、字母、符号中至少2种元素");
                      }
                    } else {
                      TostUtil.showToast("密码长度不够");
                    }
                  } else {
                    TostUtil.showToast("密码不一致");
                  }
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  static bool isCorrect(String str) {
    return new RegExp(r"(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{8,16}$")
        .hasMatch(str);
    //^(?=.*\d)(?=.*[A-Za-z])[\x20-\x7e]{8,16}$
//(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,16}$/
  }

  void onSubmission(String s) {
    InternetAPI.getInstance().post(
      "WConfiguration/PptUser/ResetUserPassword",
      (data) {
        print(data);
        TostUtil.showToast("成功");
        Navigator.pop(context);
      },
      params: {
        "password": s,
        "systemtype": "系统设置",
        "useraccount": DataSingleton().Data.useraccount,
        "username": DataSingleton().Data.username
      },
      options: new Options(
          contentType: ContentType.parse("application/x-www-form-urlencoded")),
    );
  }
}
